package com.dao;

import com.entity.reader;
import com.entity.search;
import com.utils.PropertiesUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class searchDao {

    public List<search> findall() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from search ";
        List<search> list = queryRunner.query(sql, new BeanListHandler<>(search.class));
        return list;
    }

    public List<search> findbyauthor(String author) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from search where author_name = ?";
        List<search> list = queryRunner.query(sql, new BeanListHandler<>(search.class),author);
        return list;
    }


    public search findBybookname(String bookname) throws SQLException{
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from search where bookname = ?";
        search book = queryRunner.query(sql, new BeanHandler<>(search.class),bookname);
        return book;
    }

    public void add(search newbook) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "insert into search(bookname,author_name,types,brief_introduction) values(?,?,?,?)";
        queryRunner.update(sql,newbook.getBookname(),newbook.getAuthor_name(),newbook.getTypes(),newbook.getBrief_introduction());
    }
}
